/*
 * @lc app=leetcode.cn id=2515 lang=golang
 *
 * [2515] 到目标字符串的最短距离
 */
package Solutions

// @lc code=start
func closetTarget(words []string, target string, startIndex int) int {
	indexList := make([]int, 0, 8)
	for i, v := range words {
		if v == target {
			indexList = append(indexList, i)
		}
	}
	if len(indexList) == 0 {
		return -1
	}
	var min int = (1<<63 - 1)
	for _, v := range indexList {
		if v == startIndex {
			return 0
		}
		if v < startIndex {

			b1 := (startIndex - v)
			b2 := len(words) - b1
			if b1 < min {
				min = b1
			}
			if b2 < min {
				min = b2
			}
		} else {

			b1 := v - startIndex
			b2 := len(words) - b1
			if b1 < min {
				min = b1
			}
			if b2 < min {
				min = b2
			}
		}
	}
	return min
}

// @lc code=end
